<template>
  <div>
    <el-radio-group v-model="labelPosition" size="small">
      <el-radio-button label="left">左对齐</el-radio-button>
      <el-radio-button label="right">右对齐</el-radio-button>
      <el-radio-button label="top">顶部对齐</el-radio-button>
    </el-radio-group>
    <div style="margin: 20px;"></div>
    <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
      <el-form-item label="订单编号">
        <el-input v-model="formLabelAlign.fitting_id"></el-input>
      </el-form-item>
      <el-form-item label="订单名称">
        <el-input v-model="formLabelAlign.fitting"></el-input>
      </el-form-item>
      <el-form-item label="金额">
        <el-input v-model="formLabelAlign.price" placeholder="保留两位小数"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <!-- <el-input v-model="post.status"></el-input> -->
        <el-radio-group v-model="formLabelAlign.status">
          <el-radio-button label="1">有效</el-radio-button>
          <el-radio-button label="0">无效</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生产厂商">
        <el-input v-model="formLabelAlign.cs"></el-input>
      </el-form-item>
      <el-form-item label="调整时间">
        <el-date-picker
          type="datetime"
          :clearable="false"
          range-separator="-"
          style="width: 210px"
          placeholder="选择日期"
          class="filter-items"
          v-model="formLabelAlign.add_time"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default_time="default_time"
          @change="dataChange"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="submit" @click.prevent="submitForm($event)">提交</el-button>
        <el-button @click="reset" type="reset">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import php from '../api/php.js';
export default {
  name: "Order_add",
  data() {
    return {
      name: "",
      age: "",
      labelPosition: "right",
      formLabelAlign: {
        fitting: "",
        fitting_id: "",
        add_time: "",
        price: "",
        status: "",
        cs: ""
      },
      default_time: ["00:00:00", "23:59:59"],
      post: []
    };
  },
  watch: {
    $route() {
      console.log("路由发生改变");
      this.fun();
    }
  },
  methods: {
    reset() {
      //返回上一页
      window.history.back();
    },
    dataChange(val) {
      //   console.log(`时间格式---`, val); //时间格式--- (2) ['2021-01-01', '2021-01-09', __ob__: Observer]
      const date = new Date(+new Date(val[0]) + 8 * 3600 * 1000); //new Date() 括号里的加上丢失的8小时
        // console.log(`时区格式---`, date.toISOString()); //时区格式--- 2021-01-01T00:00:00.000Z
        return date.toISOString();
    },
    //日期格式转化
    // getTime(val) {
    //   const d = new Date(val);
    //   this.F.rdate =d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
    //   var date = toString(2020);
    //   // 如果toString 不行可以使用 拼接字符串 建议使用下面的方法，toString 返回结果很可能是一个 Object
    //   var date = 2020 + "";
    // },

    submitForm(event) {
      event.preventDefault();
      let formData = new FormData();
      //获取表单数据
      formData.append("fitting_id", this.formLabelAlign.fitting_id);
      formData.append("fitting", this.formLabelAlign.fitting);
      formData.append("add_time", this.formLabelAlign.add_time);
      formData.append("price", this.formLabelAlign.price);
      formData.append("status", this.formLabelAlign.status);
      formData.append("cs", this.formLabelAlign.cs);
      //判断金额是否为数字，小数点不能超过两位，不是数字则提示错误
        if (isNaN(this.formLabelAlign.price)) {
            this.$message({
            message: "金额必须为数字",
            type: "error"
            });
            return;
        } else if (this.formLabelAlign.price.indexOf(".") != -1) {
            const price = this.formLabelAlign.price.split(".")[1];
            if (price.length > 2) {
            this.$message({
                message: "小数点后最多两位",
                type: "error"
            });
            return;
            }
        }
      //判断表单数据是否为空，如果为空，则提示用户
      if (
        this.formLabelAlign.fitting_id == "" ||
        this.formLabelAlign.fitting == "" ||
        this.formLabelAlign.add_time == "" ||
        this.formLabelAlign.price == "" ||
        this.formLabelAlign.status == "" ||
        this.formLabelAlign.cs == ""
      ) {
        this.$message({
          message: "请填写完整信息",
          type: "warning"
        });
        //终止代码往下执行
        return;
      }

      this.$axios
        .post(php+"order_add.php", formData)
        .then(res => {
          // console.log(res.data);
            if (res.data.code == 200) {
              this.$message({
                message: "修改成功",
                duration: 1000,
                type: "success"
              });
              //返回上一页
              window.history.back();
              return;
            } else {
              this.$message({
                message: "添加失败,请重试",
                type: "error",
                duration: 1500
              });
            }
        });
    }
  }
};
</script>
